import type { Buff } from "../types";
import { BuffConfig } from "../types";

// 通用buff生成器
export class BuffGenerator {
  private static generateAttributeEffect(attributeModifiers: { [key: string]: number }) {
    return (attacker: any, target: any, self: any, stackCount: number, index: number, SkillData: any) => {
      Object.entries(attributeModifiers).forEach(([attr, modifier]) => {
        const currentValue = target.getAttribute(attr) || 0;
        const newValue = currentValue + (modifier * stackCount);
        target.setAttribute(attr, newValue);
      });
    };
  }

  private static generateStatusEffect(statusEffects: { type: string; value: number }[]) {
    return (attacker: any, target: any, self: any, stackCount: number, index: number, SkillData: any) => {
      statusEffects.forEach(status => {
        // 这里可以根据状态类型实现不同的效果
        console.log(`应用状态效果: ${status.type}, 值: ${status.value * stackCount}`);
      });
    };
  }

  static createBuff(config: BuffConfig): Buff {
    let effect: any;
    let endEffect: any;

    switch (config.effectType) {
      case '属性':
        if (config.attributeModifiers) {
          effect = this.generateAttributeEffect(config.attributeModifiers);
        }
        break;
      case '状态':
        if (config.statusEffects) {
          effect = this.generateStatusEffect(config.statusEffects);
        }
        break;
      case '持续伤害':
        // 通用持续伤害buff
        effect = (attacker: any, target: any, self: any, stackCount: number, index: number, SkillData: any) => {
          const damage = (config.damage ?? SkillData.damage ?? 1) * stackCount;
          const damageType = config.damageType ?? SkillData.damageType ?? '真实攻击';
          const actualDamage = target.hit(damage, 0, damageType);
          target.setLog(attacker, target, damageType, actualDamage, self);
        };
        break;
      case '护盾':
        // 通用护盾buff
        effect = (attacker: any, target: any, self: any, stackCount: number, index: number, SkillData: any) => {
          const shieldValue = (config.shieldValue ?? SkillData.shieldValue ?? 50) * stackCount;
          // 这里调用目标的护盾相关方法
          if (target.addShield) {
            target.addShield(shieldValue);
          }
        };
        break;
    }

    if (config.endEffect) {
      endEffect = (attacker: any, target: any, self: any, stackCount: number, index: number, SkillData: any) => {
        console.log(`执行结束效果: ${config.endEffect}`);
      };
    }

    return {
      id: config.id,
      name: config.name,
      logType: config.logType,
      duration: config.duration,
      effect: effect || (() => {}),
      endEffect: endEffect
    };
  }
}

// 导出createBuff方法供其他地方使用
export const createBuff = (config: BuffConfig): Buff => BuffGenerator.createBuff(config);